package cn.genmer.test.security.common;

import com.alibaba.fastjson.JSON;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.apache.http.HttpEntity;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * yyq
 * @param <T>
 */
@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class CommonResult<T> {
    private Integer code;
    private String message;
    private T data;
    /**
     * 根据response解析数据
     *
     * @return  SuccessResult
     */
    public static CommonResult response(CloseableHttpResponse response) {
        CommonResult commonResult = new CommonResult();


        StatusLine status = response.getStatusLine();
        // 从响应模型中获取响应实体
        HttpEntity responseEntity = response.getEntity();

        // 成功
        if ("200".contains(String.valueOf(status.getStatusCode())) || "OK".contains(status.getReasonPhrase())){
            commonResult.setCode(ResultCode.SUCCESS.getCode());
            commonResult.setMessage(String.format("获取成功, 响应内容长度为: %s", responseEntity.getContentLength()));
        } else {
            // 失败
            commonResult.setCode(ResultCode.FAILED.getCode());
            commonResult.setMessage("获取失败, 详情查看data");
        }
        // 成功失败都设置返回值
        try {
            commonResult.setData(stringSerializeObject(EntityUtils.toString(responseEntity)));
        } catch (IOException e){
            e.printStackTrace();
            commonResult.setCode(ResultCode.CATCH_ERROR.getCode());
            commonResult.setMessage("获取失败, 读取IO出错");
            commonResult.setData( e.toString());

        } catch (Exception e) {
            e.printStackTrace();
            commonResult.setCode(ResultCode.CATCH_ERROR.getCode());
            commonResult.setMessage("结果字符串序列化失败");
            commonResult.setData( e.toString());
        }
        return commonResult;
    }

    /**
     * 成功但不带数据
     *
     * @return  SuccessResult
     */
    public static CommonResult success(){
        return success(null);
    }

    /**
     * 成功且带数据
     *
     * @return  SuccessResult
     */
    public static CommonResult success(Object data) {
        CommonResult commonResult = new CommonResult();
        commonResult.setCode(ResultCode.SUCCESS.getCode());
        commonResult.setMessage(ResultCode.SUCCESS.getMessage());
        commonResult.setData(data);
        return commonResult;
    }

    /**
     * 根据 ErrorCode 创建 ErrorResult
     *
     * @param error 错误类型
     * @return      ErrorResult
     */
    public static CommonResult error(ErrorCode error){
        CommonResult commonResult = new CommonResult();
        commonResult.setCode(error.getCode());
        commonResult.setMessage(error.getMessage());
        return commonResult;
    }

    /**
     * 根据 ErrorCode 创建 ErrorResult
     *
     * @param errorMsg 错误信息
     * @return      ErrorResult
     */
    public static CommonResult error(String errorMsg){
        CommonResult commonResult = new CommonResult();
        commonResult.setCode(ResultCode.FAILED.getCode());
        commonResult.setMessage(errorMsg);
        return commonResult;
    }

    /**
     * 根据错误码和消息创建 ErrorResult
     *
     * @param code  错误码
     * @param msg   消息
     * @return      ErrorResult
     */
    public static CommonResult error(Integer code, String msg){
        CommonResult commonResult = new CommonResult();
        commonResult.setCode(code);
        commonResult.setMessage(msg);
        return commonResult;
    }

    /**
     * 根据 ResultCode 创建 CommonResult
     *
     * @param result 消息类型
     * @return      CommonResult
     */
    public static CommonResult common(ResultCode result){
        CommonResult commonResult = new CommonResult();
        commonResult.setCode(result.getCode());
        commonResult.setMessage(result.getMessage());
        return commonResult;
    }

    /**
     42      * 字符串序列化为对象
     43      * @param objectStr
     44      * @return
     45      * @throws Exception
     46      */
     public static Object stringSerializeObject(String objectStr){
         Object parse = JSON.parse(objectStr);
         return parse;
     }


}
